package org.chemodansama.fifteen;

import static org.chemodansama.engine.render.imgui.NpGuiReturnConsts.active;
import static org.chemodansama.engine.render.imgui.NpGuiReturnConsts.mouseMovedIn;

import java.util.TreeMap;

import org.chemodansama.engine.NpSoundMan;
import org.chemodansama.engine.math.NpRect;
import org.chemodansama.engine.render.imgui.NpAlignConsts;
import org.chemodansama.engine.render.imgui.NpFontParams;
import org.chemodansama.engine.render.imgui.NpSkin;

public class FifteenButton 
        implements NpAlignConsts, FifteenConsts  {
    
    private final static int DEFAULT_FONT_HEIGHT = 24;
    
    private final static NpFontParams NEURO24 = 
        new NpFontParams("neuro24", DEFAULT_FONT_HEIGHT, COLOR_WHITE);
    
    /**
     * increase hovered button by HOVER_DELTA
     */
    private final static double HOVER_DELTA = 0.05;  
    private final static int BUTTON_W_NORMAL = 300;
    final static int BUTTON_H_NORMAL = 62;
    final static int BUTTON_PADDING = 36;

    private static FifteenButton mInstance = null;
    
    private final TreeMap<String, Double> mBtnSize = 
        new TreeMap<String, Double>();

    private final NpSoundMan mSoundMan;
    
    private long mRenderTimeStamp = System.currentTimeMillis();
    
    private FifteenButton(NpSoundMan sm) {
        mSoundMan = sm;
    }
    
    synchronized public static FifteenButton getInstance(NpSoundMan sm) {
        if (mInstance == null) {
            mInstance = new FifteenButton(sm);
        }
        
        return mInstance;
    }
    
    int doButton(String caption, int y, long curTime) {
        
        Double w = mBtnSize.get(caption);
        
        if (w == null) {
            w = 0.0;
        }
        
        double wf = w.doubleValue();
        
        int bw = (int) (((wf * HOVER_DELTA * BUTTON_W_NORMAL) + BUTTON_W_NORMAL));
        int hdelta = (int) (wf * HOVER_DELTA * BUTTON_H_NORMAL);
        int bh = hdelta + BUTTON_H_NORMAL;
                    
        NpRect rect = new NpRect((DEF_WIDTH - bw) / 2, y - hdelta / 2, bw, bh);
        
        NEURO24.height = (int) ((DEFAULT_FONT_HEIGHT 
                + (float) (DEFAULT_FONT_HEIGHT * HOVER_DELTA * wf)));
        
        int r = NpSkin.doButton("15Btn", caption, NEURO24, rect);

        NEURO24.height = DEFAULT_FONT_HEIGHT;
        
        double delta = 25 * (curTime - mRenderTimeStamp) / 1000.0;

        if (mouseMovedIn(r)) {
            mSoundMan.playSound(R.raw.btn);
        }
        
        if (active(r)) {
            if (wf < 1) {
                wf += delta;
                if (wf > 1) {
                    wf = 1;
                }
            } else {
                wf = 1;
            }
        } else {
            if (wf > 0) {
                wf -= delta;
                if (wf < 0) {
                    wf = 0;
                }
            } else {
                wf = 0;
            }
        }
        
        mBtnSize.put(caption, new Double(wf));
        
        return r;
    }
    
    synchronized static void postRender(long curTime) {
        if ((mInstance != null) && (curTime - mInstance.mRenderTimeStamp > 0)) {
            mInstance.mRenderTimeStamp = curTime;
        }
    }
}
